/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.buffer;

import com.aduib.boot.common.util.ObjectUtils;
import com.aduib.boot.common.util.SystemUtils;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @description: 引用计数更新器
 * @author: zzh
 * @date: 2021/12/3 10:19
 */
public abstract class ReferenceCountedUpdater<T extends ReferenceCounted> {

  public static long getUnsafeOffset(Class<? extends ReferenceCounted> clz, String fieldName) {
    try {
      if (SystemUtils.hasUnsafe()) {
        return SystemUtils.objectFieldOffset(clz.getDeclaredField(fieldName));
      }
    } catch (Throwable ignore) {
      // fall-back
    }
    return -1;
  }

  private static int realRefCnt(int rawCnt) {
    return rawCnt != 2 && rawCnt != 4 && (rawCnt & 1) != 0 ? 0 : rawCnt >>> 1;
  }

  /** Like {@link #realRefCnt(int)} but throws if refCnt == 0 */
  private static int toLiveRealRefCnt(int rawCnt, int decrement) {
    if (rawCnt == 2 || rawCnt == 4 || (rawCnt & 1) == 0) {
      return rawCnt >>> 1;
    }
    // odd rawCnt => already deallocated
    throw new IllegalReferenceCountException(0, -decrement);
  }

  protected abstract AtomicIntegerFieldUpdater<T> updater();

  protected abstract long unsafeOffset();

  public final int initialValue() {
    return 2;
  }

  private int nonVolatileRawCnt(T instance) {
    // TODO: Once we compile against later versions of Java we can replace the Unsafe usage here by
    // varhandles.
    final long offset = unsafeOffset();
    return offset != -1 ? SystemUtils.getInt(instance, offset) : updater().get(instance);
  }

  public final int refCnt(T instance) {
    return realRefCnt(updater().get(instance));
  }

  public final boolean isLiveNonVolatile(T instance) {
    final long offset = unsafeOffset();
    final int rawCnt =
        offset != -1 ? SystemUtils.getInt(instance, offset) : updater().get(instance);

    // The "real" ref count is > 0 if the rawCnt is even.
    return rawCnt == 2 || rawCnt == 4 || rawCnt == 6 || rawCnt == 8 || (rawCnt & 1) == 0;
  }

  /** An unsafe operation that sets the reference count directly */
  public final void setRefCnt(T instance, int refCnt) {
    updater().set(instance, refCnt > 0 ? refCnt << 1 : 1); // overflow OK here
  }

  /** Resets the reference count to 1 */
  public final void resetRefCnt(T instance) {
    updater().set(instance, initialValue());
  }

  public final T retain(T instance) {
    return retain0(instance, 1, 2);
  }

  public final T retain(T instance, int increment) {
    // all changes to the raw count are 2x the "real" change - overflow is OK
    int rawIncrement = ObjectUtils.checkPositive(increment, "increment") << 1;
    return retain0(instance, increment, rawIncrement);
  }

  // rawIncrement == increment << 1
  private T retain0(T instance, final int increment, final int rawIncrement) {
    int oldRef = updater().getAndAdd(instance, rawIncrement);
    if (oldRef != 2 && oldRef != 4 && (oldRef & 1) != 0) {
      throw new IllegalReferenceCountException(0, increment);
    }
    // don't pass 0!
    if ((oldRef <= 0 && oldRef + rawIncrement >= 0)
        || (oldRef >= 0 && oldRef + rawIncrement < oldRef)) {
      // overflow case
      updater().getAndAdd(instance, -rawIncrement);
      throw new IllegalReferenceCountException(realRefCnt(oldRef), increment);
    }
    return instance;
  }

  public final boolean release(T instance) {
    int rawCnt = nonVolatileRawCnt(instance);
    return rawCnt == 2
        ? tryFinalRelease0(instance, 2) || retryRelease0(instance, 1)
        : nonFinalRelease0(instance, 1, rawCnt, toLiveRealRefCnt(rawCnt, 1));
  }

  public final boolean release(T instance, int decrement) {
    int rawCnt = nonVolatileRawCnt(instance);
    int realCnt = toLiveRealRefCnt(rawCnt, ObjectUtils.checkPositive(decrement, "decrement"));
    return decrement == realCnt
        ? tryFinalRelease0(instance, rawCnt) || retryRelease0(instance, decrement)
        : nonFinalRelease0(instance, decrement, rawCnt, realCnt);
  }

  private boolean tryFinalRelease0(T instance, int expectRawCnt) {
    return updater().compareAndSet(instance, expectRawCnt, 1); // any odd number will work
  }

  private boolean nonFinalRelease0(T instance, int decrement, int rawCnt, int realCnt) {
    if (decrement < realCnt
        // all changes to the raw count are 2x the "real" change - overflow is OK
        && updater().compareAndSet(instance, rawCnt, rawCnt - (decrement << 1))) {
      return false;
    }
    return retryRelease0(instance, decrement);
  }

  private boolean retryRelease0(T instance, int decrement) {
    for (; ; ) {
      int rawCnt = updater().get(instance), realCnt = toLiveRealRefCnt(rawCnt, decrement);
      if (decrement == realCnt) {
        if (tryFinalRelease0(instance, rawCnt)) {
          return true;
        }
      } else if (decrement < realCnt) {
        // all changes to the raw count are 2x the "real" change
        if (updater().compareAndSet(instance, rawCnt, rawCnt - (decrement << 1))) {
          return false;
        }
      } else {
        throw new IllegalReferenceCountException(realCnt, -decrement);
      }
      Thread.yield(); // this benefits throughput under high contention
    }
  }
}
